package org.footballmanager.pandafootball.common.util;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.footballmanager.pandafootball.common.exception.ExceptionEnum;
import org.footballmanager.pandafootball.common.exception.MyException;
import org.footballmanager.pandafootball.system.domain.FootTableTitle;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

public class ExportExcelSpecial {
    public ExportExcelSpecial() {
    }

    public static Workbook createWorkbook() {
        return new SXSSFWorkbook(500);
    }

    public static Workbook createWorkbook(InputStream in) throws IOException {
        try {
            return new HSSFWorkbook(in);
        } catch (Exception var4) {
            try {
                return new XSSFWorkbook(in);
            } catch (IOException e) {
                throw new MyException(ExceptionEnum.INPUTEXCEPTION, e);
            }
        }
    }

    public static void addHeader(Row headerRow, CellStyle style, List<FootTableTitle> list) {
        int column = 0;
        for (FootTableTitle header : list) {
            addCell(headerRow, column++, header.getName(), style);
        }
    }

    public static Cell addCell(Row row, int column, Object val, CellStyle style) {
        Cell cell = row.createCell(column);

        try {
            if (val == null) {
                cell.setCellValue("");
            } else if (val instanceof String) {
                cell.setCellValue((String) val);
            } else if (val instanceof Integer) {
                cell.setCellValue((double) ((Integer) val).intValue());
            } else if (val instanceof Long) {
                cell.setCellValue((double) ((Long) val).longValue());
            } else if (val instanceof Double) {
                style.setDataFormat((short) 2);
                cell.setCellValue(((Double) val).doubleValue());
            } else if (val instanceof Float) {
                cell.setCellValue((double) ((Float) val).floatValue());
            } else if (val instanceof Date) {
                cell.setCellValue(DateUtils.dateToString((Date) val, "yyyy-MM-dd"));
            } else if (val instanceof BigDecimal) {
                cell.setCellValue(((BigDecimal) val).doubleValue());
            }
        } catch (Exception var6) {
            cell.setCellValue(val.toString());
        }

        cell.setCellStyle(style);
        return cell;
    }

    public static CellStyle getTitleStyle(Workbook wb) {
        CellStyle titleStyle = wb.createCellStyle();
        titleStyle.setBorderRight((short) 1);
        titleStyle.setRightBorderColor(IndexedColors.GREY_80_PERCENT.getIndex());
        titleStyle.setBorderLeft((short) 1);
        titleStyle.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        titleStyle.setBorderTop((short) 1);
        titleStyle.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        titleStyle.setBorderBottom((short) 1);
        titleStyle.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        titleStyle.setAlignment((short) 2);
        titleStyle.setVerticalAlignment((short) 1);
        Font titleFont = wb.createFont();
        titleFont.setFontName("Arial");
        titleFont.setFontHeightInPoints((short) 10);
        titleFont.setBoldweight((short) 700);
        titleStyle.setFont(titleFont);
        return titleStyle;
    }

    public static CellStyle getHeaderStyle(Workbook wb) {
        CellStyle headerStyle = wb.createCellStyle();
        headerStyle.setBorderRight((short) 1);
        headerStyle.setRightBorderColor(IndexedColors.GREY_80_PERCENT.getIndex());
        headerStyle.setBorderLeft((short) 1);
        headerStyle.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        headerStyle.setBorderTop((short) 1);
        headerStyle.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        headerStyle.setBorderBottom((short) 1);
        headerStyle.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        headerStyle.setAlignment((short) 2);
        headerStyle.setVerticalAlignment((short) 1);
        headerStyle.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
        headerStyle.setFillPattern((short) 1);
        Font headerFont = wb.createFont();
        headerFont.setFontName("Arial");
        headerFont.setFontHeightInPoints((short) 10);
        headerFont.setBoldweight((short) 700);
        headerFont.setColor(IndexedColors.WHITE.getIndex());
        headerStyle.setFont(headerFont);
        return headerStyle;
    }

    public static CellStyle getCellStyle(Workbook wb) {
        CellStyle cellStyle = wb.createCellStyle();
        cellStyle.setBorderRight((short) 1);
        cellStyle.setRightBorderColor(IndexedColors.GREY_80_PERCENT.getIndex());
        cellStyle.setBorderLeft((short) 1);
        cellStyle.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        cellStyle.setBorderTop((short) 1);
        cellStyle.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        cellStyle.setBorderBottom((short) 1);
        cellStyle.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        cellStyle.setAlignment((short) 2);
        cellStyle.setVerticalAlignment((short) 1);
        Font titleFont = wb.createFont();
        titleFont.setFontName("Arial");
        titleFont.setFontHeightInPoints((short) 10);
        cellStyle.setFont(titleFont);
        return cellStyle;
    }

    public static void writer(Workbook wb, OutputStream outputStream) throws IOException {
        wb.write(outputStream);
    }

    public static String getCellValue(Cell cell) {
        Object val = "";
        try {
            if (cell != null) {
                if (cell.getCellType() == 0) {
                    val = cell.getNumericCellValue();
                } else if (cell.getCellType() == 1) {
                    val = cell.getStringCellValue();
                } else if (cell.getCellType() == 2) {
                    val = cell.getCellFormula();
                } else if (cell.getCellType() == 4) {
                    val = cell.getBooleanCellValue();
                } else if (cell.getCellType() == 5) {
                    val = cell.getErrorCellValue();
                }
            }
        } catch (Exception e) {
            throw new MyException(ExceptionEnum.INPUTEXCEPTION, e);
        }

        return String.valueOf(val).trim();
    }

    /**
     * 获取输出流
     *
     * @param fileName 文件名
     * @param request  请求
     * @param response 响应
     * @return 输出流
     * @throws IOException
     */
    public static ServletOutputStream getResponseOutputStream(String fileName, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String fileName_request = getFileName(request, fileName);
        response.reset();
        response.setContentType("application/octet-stream; charset=utf-8");
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName_request);
        return response.getOutputStream();
    }

    /**
     * 获取文件名称
     *
     * @param fileName 文件名
     * @return 文件名
     */
    public static String getFileName(HttpServletRequest request, String fileName) {
        try {
            String agent = request.getHeader("User-Agent");
            boolean isMSIE = (agent != null && agent.contains("MSIE"));
            if (isMSIE) {
                return URLEncoder.encode(fileName, "UTF8");
            } else {
                return new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
            }
        } catch (UnsupportedEncodingException e) {
            return fileName;
        }
    }
}
